package com.bjpowernode.rbac.service.impl;

import com.bjpowernode.rbac.entity.CustomerTraceHistory;
import com.bjpowernode.rbac.entity.CustomerTransfer;
import com.bjpowernode.rbac.entity.Employee;
import com.bjpowernode.rbac.entity.SystemDictionaryItem;
import com.bjpowernode.rbac.mapper.CustomerTraceHistoryMapper;
import com.bjpowernode.rbac.mapper.CustomerTransferMapper;
import com.bjpowernode.rbac.query.HistoryQuery;
import com.bjpowernode.rbac.service.CustomerTraceHistoryService;
import com.bjpowernode.rbac.util.DateUtils;
import com.bjpowernode.rbac.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author jy
 * @version 1.0
 * @date 2022/2/11 16:55
 */
@Service
public class CustomerTraceHistoryServiceImpl implements CustomerTraceHistoryService {

    @Autowired
    private CustomerTraceHistoryMapper customerTraceHistoryMapper;

    @Autowired
    private CustomerTransferMapper customerTransferMapper;

    //查询用户已有的跟进记录
    @Override
    public PageInfo<SystemDictionaryItem> selectTraceType() {
        List<SystemDictionaryItem> list = customerTraceHistoryMapper.selectAllTraceType();
        return new PageInfo<>(list);
    }

    //查询用户已有的跟进结果
    @Override
    public PageInfo<CustomerTraceHistory> selectTraceResult() {
        List<CustomerTraceHistory> list = customerTraceHistoryMapper.selectAllTraceResult();
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<CustomerTraceHistory> selectType() {
        List<CustomerTraceHistory> list = customerTraceHistoryMapper.selectAllType();
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<CustomerTraceHistory> selectTraceHistoryByCondition(HistoryQuery historyQuery) {
        PageHelper.startPage(historyQuery.getCurrentPage(),historyQuery.getPageSize());
        List<CustomerTraceHistory> list = customerTraceHistoryMapper.selectTraceHistoryByCondition(historyQuery);
        return new PageInfo<>(list);
    }

    @Override
    public void followCustomerEdit(CustomerTraceHistory customerTraceHistory,String traceTimeStr) {
        /**
         * 潜在用户跟进记录添加
         */
        Date date = DateUtils.getDate2(traceTimeStr);
        customerTraceHistory.setTraceTime(date);
        customerTraceHistory.setInputTime(new Date());
        customerTraceHistory.setInputUserId(UserContext.getEmployee().getId());
        customerTraceHistoryMapper.insert(customerTraceHistory);
    }

    /**
     * 移交(对于移交历史记录表的增加)
     * @param customerTransfer
     */
    @Override
    public void transfer(CustomerTransfer customerTransfer) {
        customerTransfer.setOperateTime(new Date());
        customerTransfer.setOperatorId(UserContext.getEmployee().getId());
        customerTransferMapper.insert(customerTransfer);
    }

    /**
     * 跟进历史编辑
     * @param customerTraceHistory
     */
    @Override
    public void update(CustomerTraceHistory customerTraceHistory,String traceTimeStr) {
        Date date = DateUtils.getDate2(traceTimeStr);
        customerTraceHistory.setTraceTime(date);
        customerTraceHistoryMapper.updateByPrimaryKeySelective(customerTraceHistory);
    }
}
